home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 405_01 / flexpp / sym.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-04  |  7.4 KB  |  317 lines

  1. /* sym - symbol table routines */
  2.  
  3. /*-
  4.  * Copyright (c) 1990 The Regents of the University of California.
  5.  * All rights reserved.
  6.  *
  7.  * This code is derived from software contributed to Berkeley by
  8.  * Vern Paxson.
  9.  * 
  10.  * The United States Government has rights in this work pursuant
  11.  * to contract no. DE-AC03-76SF00098 between the United States
  12.  * Department of Energy and the University of California.
  13.  *
  14.  * Redistribution and use in source and binary forms are permitted provided
  15.  * that: (1) source distributions retain this entire copyright notice and
  16.  * comment, and (2) distributions including binaries display the following
  17.  * acknowledgement:  ``This product includes software developed by the
  18.  * University of California, Berkeley and its contributors'' in the
  19.  * documentation or other materials provided with the distribution and in
  20.  * all advertising materials mentioning features or use of this software.
  21.  * Neither the name of the University nor the names of its contributors may
  22.  * be used to endorse or promote products derived from this software without
  23.  * specific prior written permission.
  24.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  25.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  26.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  27.  */
  28.  
  29. #ifndef lint
  30. static char rcsid[] =
  31.     "@(#) $Header: /usr/fsys/odin/a/vern/flex/RCS/sym.c,v 2.4 90/06/27 23:48:36 vern Exp $ (LBL)";
  32. #endif
  33.  
  34. #include "flexdef.h"
  35.  
  36.  
  37. /* declare functions that have forward references */
  38.  
  39. int hashfunct PROTO((register char[], int));
  40.  
  41.  
  42. struct hash_entry *ndtbl[NAME_TABLE_HASH_SIZE];
  43. struct hash_entry *sctbl[START_COND_HASH_SIZE];
  44. struct hash_entry *ccltab[CCL_HASH_SIZE];
  45.  
  46. struct hash_entry *findsym();
  47.  
  48.  
  49. /* addsym - add symbol and definitions to symbol table
  50.  *
  51.  * synopsis
  52.  *    char sym[], *str_def;
  53.  *    int int_def;
  54.  *    hash_table table;
  55.  *    int table_size;
  56.  *    0 / -1 = addsym( sym, def, int_def, table, table_size );
  57.  *
  58.  * -1 is returned if the symbol already exists, and the change not made.
  59.  */
  60.  
  61.  
  62. int addsym( sym, str_def, int_def, table, table_size )
  63. register char sym[];
  64. char *str_def;
  65. int int_def;
  66. hash_table table;
  67. int table_size;
  68.  
  69.     {
  70.     int hash_val = hashfunct( sym, table_size );
  71.     register struct hash_entry *sym_entry = table[hash_val];
  72.     register struct hash_entry *new_entry;
  73.     register struct hash_entry *successor;
  74.  
  75.     while ( sym_entry )
  76.     {
  77.     if ( ! strcmp( sym, sym_entry->name ) )
  78.         { /* entry already exists */
  79.         return ( -1 );
  80.         }
  81.     
  82.     sym_entry = sym_entry->next;
  83.     }
  84.  
  85.     /* create new entry */
  86.     new_entry = (struct hash_entry *) malloc( sizeof( struct hash_entry ) );
  87.  
  88.     if ( new_entry == NULL )
  89.     flexfatal( "symbol table memory allocation failed" );
  90.  
  91.     if ( (successor = table[hash_val]) )
  92.     {
  93.     new_entry->next = successor;
  94.     successor->prev = new_entry;
  95.     }
  96.     else
  97.     new_entry->next = NULL;
  98.  
  99.     new_entry->prev = NULL;
  100.     new_entry->name = sym;
  101.     new_entry->str_val = str_def;
  102.     new_entry->int_val = int_def;
  103.  
  104.     table[hash_val] = new_entry;
  105.  
  106.     return ( 0 );
  107.     }
  108.  
  109.  
  110. /* cclinstal - save the text of a character class
  111.  *
  112.  * synopsis
  113.  *    Char ccltxt[];
  114.  *    int cclnum;
  115.  *    cclinstal( ccltxt, cclnum );
  116.  */
  117.  
  118. void cclinstal( ccltxt, cclnum )
  119. Char ccltxt[];
  120. int cclnum;
  121.  
  122.     {
  123.     /* we don't bother checking the return status because we are not called
  124.      * unless the symbol is new
  125.      */
  126.     Char *copy_unsigned_string();
  127.  
  128.     (void) addsym( (char *) copy_unsigned_string( ccltxt ), (char *) 0, cclnum,
  129.            ccltab, CCL_HASH_SIZE );
  130.     }
  131.  
  132.  
  133. /* ccllookup - lookup the number associated with character class text
  134.  *
  135.  * synopsis
  136.  *    Char ccltxt[];
  137.  *    int ccllookup, cclval;
  138.  *    cclval/0 = ccllookup( ccltxt );
  139.  */
  140.  
  141. int ccllookup( ccltxt )
  142. Char ccltxt[];
  143.  
  144.     {
  145.     return ( findsym( (char *) ccltxt, ccltab, CCL_HASH_SIZE )->int_val );
  146.     }
  147.  
  148.  
  149. /* findsym - find symbol in symbol table
  150.  *
  151.  * synopsis
  152.  *    char sym[];
  153.  *    hash_table table;
  154.  *    int table_size;
  155.  *    struct hash_entry *sym_entry, *findsym();
  156.  *    sym_entry = findsym( sym, table, table_size );
  157.  */
  158.  
  159. struct hash_entry *findsym( sym, table, table_size )
  160. register char sym[];
  161. hash_table table;
  162. int table_size;
  163.  
  164.     {
  165.     register struct hash_entry *sym_entry = table[hashfunct( sym, table_size )];
  166.     static struct hash_entry empty_entry =
  167.     {
  168.     (struct hash_entry *) 0, (struct hash_entry *) 0, NULL, NULL, 0,
  169.     } ;
  170.  
  171.     while ( sym_entry )
  172.     {
  173.     if ( ! strcmp( sym, sym_entry->name ) )
  174.         return ( sym_entry );
  175.     sym_entry = sym_entry->next;
  176.     }
  177.  
  178.     return ( &empty_entry );
  179.     }
  180.  
  181.     
  182. /* hashfunct - compute the hash value for "str" and hash size "hash_size"
  183.  *
  184.  * synopsis
  185.  *    char str[];
  186.  *    int hash_size, hash_val;
  187.  *    hash_val = hashfunct( str, hash_size );
  188.  */
  189.  
  190. int hashfunct( str, hash_size )
  191. register char str[];
  192. int hash_size;
  193.  
  194.     {
  195.     register int hashval;
  196.     register int locstr;
  197.  
  198.     hashval = 0;
  199.     locstr = 0;
  200.  
  201.     while ( str[locstr] )
  202.     hashval = ((hashval << 1) + (unsigned char) str[locstr++]) % hash_size;
  203.  
  204.     return ( hashval );
  205.     }
  206.  
  207.  
  208. /* ndinstal - install a name definition
  209.  *
  210.  * synopsis
  211.  *    char nd[];
  212.  *    Char def[];
  213.  *    ndinstal( nd, def );
  214.  */
  215.  
  216. void ndinstal( nd, def )
  217. char nd[];
  218. Char def[];
  219.  
  220.     {
  221.     char *copy_string();
  222.     Char *copy_unsigned_string();
  223.  
  224.     if ( addsym( copy_string( nd ), (char *) copy_unsigned_string( def ), 0,
  225.          ndtbl, NAME_TABLE_HASH_SIZE ) )
  226.     synerr( "name defined twice" );
  227.     }
  228.  
  229.  
  230. /* ndlookup - lookup a name definition
  231.  *
  232.  * synopsis
  233.  *    char nd[], *def;
  234.  *    char *ndlookup();
  235.  *    def/NULL = ndlookup( nd );
  236.  */
  237.  
  238. Char *ndlookup( nd )
  239. char nd[];
  240.  
  241.     {
  242.     return ( (Char *) findsym( nd, ndtbl, NAME_TABLE_HASH_SIZE )->str_val );
  243.     }
  244.  
  245.  
  246. /* scinstal - make a start condition
  247.  *
  248.  * synopsis
  249.  *    char str[];
  250.  *    int xcluflg;
  251.  *    scinstal( str, xcluflg );
  252.  *
  253.  * NOTE
  254.  *    the start condition is Exclusive if xcluflg is true
  255.  */
  256.  
  257. void scinstal( str, xcluflg )
  258. char str[];
  259. int xcluflg;
  260.  
  261.     {
  262.     char *copy_string();
  263.  
  264.     /* bit of a hack.  We know how the default start-condition is
  265.      * declared, and don't put out a define for it, because it
  266.      * would come out as "#define 0 1"
  267.      */
  268.     /* actually, this is no longer the case.  The default start-condition
  269.      * is now called "INITIAL".  But we keep the following for the sake
  270.      * of future robustness.
  271.      */
  272.  
  273.     if ( strcmp( str, "0" ) )
  274.     printf( "#define %s %d\n", str, lastsc );
  275.  
  276.     if ( ++lastsc >= current_max_scs )
  277.     {
  278.     current_max_scs += MAX_SCS_INCREMENT;
  279.  
  280.     ++num_reallocs;
  281.  
  282.     scset = reallocate_integer_array( scset, current_max_scs );
  283.     scbol = reallocate_integer_array( scbol, current_max_scs );
  284.     scxclu = reallocate_integer_array( scxclu, current_max_scs );
  285.     sceof = reallocate_integer_array( sceof, current_max_scs );
  286.     scname = reallocate_char_ptr_array( scname, current_max_scs );
  287.     actvsc = reallocate_integer_array( actvsc, current_max_scs );
  288.     }
  289.  
  290.     scname[lastsc] = copy_string( str );
  291.  
  292.     if ( addsym( scname[lastsc], (char *) 0, lastsc,
  293.          sctbl, START_COND_HASH_SIZE ) )
  294.     format_pinpoint_message( "start condition %s declared twice", str );
  295.  
  296.     scset[lastsc] = mkstate( SYM_EPSILON );
  297.     scbol[lastsc] = mkstate( SYM_EPSILON );
  298.     scxclu[lastsc] = xcluflg;
  299.     sceof[lastsc] = false;
  300.     }
  301.  
  302.  
  303. /* sclookup - lookup the number associated with a start condition
  304.  *
  305.  * synopsis
  306.  *    char str[], scnum;
  307.  *    int sclookup;
  308.  *    scnum/0 = sclookup( str );
  309.  */
  310.  
  311. int sclookup( str )
  312. char str[];
  313.  
  314.     {
  315.     return ( findsym( str, sctbl, START_COND_HASH_SIZE )->int_val );
  316.     }
  317.